package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.AdvPosDto;
import com.itheima.entity.YddAdvimg;
import com.itheima.entity.YddAdvpos;

import com.itheima.service.AdvPosService;
import com.itheima.service.AdvimgService;
import com.itheima.util.MyPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advpos")
public class AdvPosController {
    @Autowired
    private AdvPosService advPosService;

    @Autowired
    private AdvimgService advImgService;

    @GetMapping("/list")
    public R<MyPage> list(Integer page,Integer limit,String advposdesc, String advposname){
        Page<YddAdvpos> pageInfo = new Page<>();
        MyPage<AdvPosDto> advposMyPage = new MyPage<>();
        advposMyPage.setPageSize(limit);
        advposMyPage.setCurrentPage(page);
        advposMyPage.setCount(pageInfo.getTotal());
        advposMyPage.setTotalPages(pageInfo.getPages());
        LambdaQueryWrapper<YddAdvpos> yddAdvposLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddAdvposLambdaQueryWrapper.like(advposname !=null,YddAdvpos::getAdvposname,advposname);
        yddAdvposLambdaQueryWrapper.orderByDesc(YddAdvpos::getAdvposdesc);
        advPosService.page(pageInfo,yddAdvposLambdaQueryWrapper);



        List<YddAdvpos> records = pageInfo.getRecords();
        List<AdvPosDto> list= new ArrayList<>();
        for (YddAdvpos record : records) {
            AdvPosDto advPosDto = new AdvPosDto();
            BeanUtils.copyProperties(record,advPosDto);
            LambdaQueryWrapper<YddAdvimg> yddAdvimgLambdaQueryWrapper = new LambdaQueryWrapper<>();
            yddAdvimgLambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos,record.getId());
            int count = advImgService.count(yddAdvimgLambdaQueryWrapper);
            advPosDto.setAdvposnum(count);
            list.add(advPosDto);
        }
        advposMyPage.setData(list);
        return R.success(advposMyPage);
    }

    @GetMapping("/rowInfo")
    public R<YddAdvpos> rowInfo(Integer id){
        YddAdvpos yddAdvpos = advPosService.getById(id);
        return R.success(yddAdvpos);
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody YddAdvpos yddAdvpos){
        advPosService.save(yddAdvpos);
        return R.success("添加成功");
    }
    @GetMapping("/del")
    public R<String> delete(String id){
        advPosService.removeById(id);
        return R.success("删除成功");
    }
    @GetMapping("/all")
    public R<List<YddAdvpos>> all(){
        List<YddAdvpos> advposList = advPosService.list();
        return R.success(advposList);
    }
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvpos yddAdvpos){
        advPosService.updateById(yddAdvpos);
        return R.success("修改成功");

    }
}
